package reflect;

import java.lang.reflect.Constructor;

/**
 * 反射方式实例化 - 类对象.getConstructor()方式;
 */
public class Demo03Reflect {
    public static void main(String[] args) throws Exception{
        //反射方式
        Class cls = Class.forName("reflect.Person");
        //1.利用无参构造器实例化;
        Constructor constructor1 = cls.getConstructor();
        Object obj1 = constructor1.newInstance();
        System.out.println(obj1);
        //2.利用1个参数的构造器实例化;
        Constructor constructor2 = cls.getConstructor(String.class);
        Object obj2 = constructor2.newInstance("亚瑟");
        System.out.println(obj2);
        //3.利用2个参数的构造器实例化;
        Constructor constructor3 = cls.getConstructor(String.class, int.class);
        Object obj3 = constructor3.newInstance("后裔", 22);
        System.out.println(obj3);
    }
}










